<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%
    Integer uid = token.getUid();
    Integer pid = input.getInt("pid", -1);
    Integer serviceId = input.getInt("server_id", -1);
    Integer scheduleId = input.getInt("schedule_id", -1);
    String date = input.getDesc("date", "");
    Integer half = input.getInt("half", 0);
    Integer type = input.getInt("type", 1);      // 普通预约1   专家预约2
    String startTime = input.getDesc("start_time", date);
    String endTime = input.getDesc("end_time", "");
    if ("".equals(date)) {
        msg = new Message(30002, "请选择预约时间");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("id=", pid).and("uid=", token.getUid())).query().getResultObj();
    if (isEmpty(userProfiles)) {
        msg = new Message(30041, "用户信息错误");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<Service> services = QBuilder.db("basedb").select().from("Service").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", serviceId)).query().getResultObj();
    if (isEmpty(services)) {
        msg = new Message(30041, "服务异常");
        Tools.encryptPrint(out, msg);
        return;
    }
    Date d = DateUtil.parseDate(date);
    Long execTime = d.getTime();
    Service service = services.get(0);
    UserProfile up = userProfiles.get(0);
    String name = Tools.getDesc(service.getName(), "");
    List<ServiceSchedule> schedules = QBuilder.db("basedb").select().from("ServiceSchedule").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("id=", scheduleId)).query().getResultObj();
    Order order = new Order();
    order.setHospital(hospital);
    order.setServiceId(serviceId);
    order.setTitle(service.getName());
    order.setConfirm(0);
    order.setType(type);
    order.setUid(up.getId());
    order.setUserName(up.getName());
    order.setRegisterTime(new Date().getTime());
    order.setOrderPay(service.getPrice());
    order.setTotalPay(service.getPrice());
    order.setState(1);
    order.setStartTime(startTime);
    order.setEndTime(endTime);
    if (!isEmpty(schedules)) {
        ServiceSchedule schedule = schedules.get(0);
        Integer doctorUid = schedule.getDoctorUid();
        String doctorName = schedule.getDoctorName();
        half = schedule.getHalf();
        order.setExecTime(execTime);
        order.setType(2);
        order.setDoctor(doctorUid);
        order.setDoctorName(doctorName);
    }
    if (half != 0) {
        order.setHalf(half);
    }
    Integer id = (Integer)DBTools.db("basedb").addItem(order);
    if (isEmpty(id)) {
        msg = new Message(30041, "预约服务失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    order.setId(id);
    createMsg(order, uid);
    msg.put("data", id);
    Tools.encryptPrint(out, msg);
%>
